/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.edu.shengda;

/**
 *
 * @author apple
 */
class ClassRoom implements Runnable{
    Thread student,teacher;

    public ClassRoom() {
        teacher = new Thread(this);
        student = new Thread(this);
        teacher.setName("王教授");
        student.setName("张三");
    }
    public void run(){
        if(Thread.currentThread()==student){
            try {
                System.out.println(student.getName()+"正在睡觉，不听课");
                Thread.sleep(1000*60*60);
            } catch (Exception e) {
               System.out.println(student.getName()+"老师被叫醒"); 
            }
            System.out.println(student.getName()+"开始听课");
        }
        else if (Thread.currentThread()==teacher) {
            for (int i = 0; i < 3; i++) {
                System.out.println("上课");
                try {
                    Thread.sleep(500);
                } catch (Exception e) {
                }
            }
            student.interrupt();
        }
    }
    
}
public class Example12_6 {
    public static void main(String[] args) {
        ClassRoom room1005   =  new ClassRoom();
        room1005.student.start();
        room1005.teacher.start();
    }
}
